﻿using System;
using System.Collections.Generic;
using System.Linq;
using Business.DbConnect;

namespace Business
{
    public class MotorBusiness
    {
        static QLNXeEntities entity = new QLNXeEntities();

        /// <summary>
        ///
        /// </summary>
        /// <param name="moto"></param>
        /// <returns></returns>
        public static bool Insert(Motorbike moto)
        {
            try
            {
                entity.Motorbikes.Add(moto);
                entity.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// Select a Motorbike with plate
        /// </summary>
        /// <param name="plate"></param>
        /// <returns></returns>
        public static Motorbike Select(string plate)
        {
            try
            {
                return entity.Motorbikes.Find(plate);
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// Select All Motorbike
        /// </summary>
        /// <returns></returns>
        public static List<Motorbike> SelectAll()
        {
            try
            {
                return entity.Motorbikes.ToList();
            }
            catch (Exception)
            {
                return new List<Motorbike>();
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="moto"></param>
        /// <returns></returns>
        public static bool Update(Motorbike moto)
        {
            try
            {
                var currentMoto = entity.Motorbikes.Find(moto.Plate);
                entity.Entry(currentMoto).CurrentValues.SetValues(moto);
                entity.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="searchText"></param>
        /// <returns></returns>
        public static IQueryable<String> Suggestion(string searchText)
        {
            try
            {
                QLNXeEntities entities = new QLNXeEntities();
                var query = from pl in entities.Motorbikes
                            select pl.Plate;
                var namelist = query.Where(n => n.ToLower().StartsWith(searchText.ToLower()));
                return namelist;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}